package com.swp.oa.sys.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.swp.oa.common.pageModel.EasyuiTreeNode;
import com.swp.oa.common.pageModel.JsonMessage;
import com.swp.oa.sys.dto.DepartmentDto;
import com.swp.oa.sys.service.IDepartmentService;

@Controller
@RequestMapping("/sys/department")
public class DepartmentController {

	@Resource
	private IDepartmentService departmentService;

	// 返回部门管理首页
	@RequestMapping()
	public String toDepartmentIndex() {
		return "redirect:/sys/department/index";
	}
	
	@RequestMapping("/index")
	public String departmentIndex() {
		return "/sys/department/index";
	}
	
	@RequestMapping("/treeGrid")
	@ResponseBody
	public List<DepartmentDto> treeGrid(long id) {
		List<DepartmentDto> deptList = departmentService.getChildrenByDepartmentId(id);
		return deptList;
	}
	
	@RequestMapping("/tree")
	@ResponseBody
	public List<EasyuiTreeNode> tree(long id) {
		List<EasyuiTreeNode> treeNodes = departmentService.tree(id);
		return treeNodes;
	}

	/**
	 * 是否创建部门
	 * @param model
	 * @return
	 */
	@RequestMapping("/toCreateDepartment")
	public String toCreateDepartment(Model model,String parentId) {
		model.addAttribute("parentId", parentId);
		return "/department/createDepartment";
	}

	/**
	 * 创建部门
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/createDepartment",method=RequestMethod.POST)
	public JsonMessage CreateDepartment(HttpServletRequest request,DepartmentDto dept) {
//		System.out.println(dept.toString());
		JsonMessage jsonMsg = new JsonMessage();
//		if(dept.getParentId() == null || "".equals(dept.getParentId())){
//			DepartmentDto deptModel = departmentService.getRootDepartmentDto();
//			if(deptModel != null){
//				jsonMsg.setSuccess(false);
//				jsonMsg.setMsg("创建部门失败");
//				jsonMsg.setObj("不能创建两个根部门");
//				return jsonMsg;
//			}
//		}
		try {
			departmentService.save(dept);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("创建部门失败！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("创建部门成功！");
		return jsonMsg;
	}

	/**
	 * 是否编辑部门
	 * 
	 * @param model
	 * @param deptId
	 * @return
	 */
	@RequestMapping("/toEditDepartment")
	public String toEditDepartment(Model model, long deptId) {
		DepartmentDto dept=departmentService.getByDeptId(deptId);
		model.addAttribute("deptId", deptId);
		model.addAttribute("parentId", dept.getParentId());
		return "/department/EditDepartment";
	}

	// 返回编辑部门数据
	@RequestMapping(value = "/editDeptForm")
	@ResponseBody
	public DepartmentDto editDeptForm(long deptId) {
		DepartmentDto model = departmentService.getByDeptId(deptId);
		DepartmentDto parentModel = departmentService.getByDeptId(model.getParentId());
		if(parentModel != null){
			model.setParentName(parentModel.getName());			
		}
//		System.out.println("editDeptForm: " + model.toString());
		return model;
	}

	/**
	 * 编辑部门
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/editDepartment")
	@ResponseBody
	public JsonMessage EditDepartment(HttpServletRequest request,DepartmentDto dept) {
//		System.out.println("come in EditDepartment " + dept.toString());
		JsonMessage jsonMsg = new JsonMessage();
		try {
			departmentService.update(dept);			
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("编辑部门失败！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("编辑部门成功！");
		return jsonMsg;
	}

	/**
	 * 删除部门
	 * @param departmentIds
	 * @return
	 */
	@RequestMapping("/deleteDepartment")
	@ResponseBody
	public JsonMessage deleteDepartment(long departmentId) {
		JsonMessage jsonMsg = new JsonMessage();
		try {
			departmentService.deleteById(departmentId);	
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("删除部门失败!");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("删除部门成功!");
		return jsonMsg;
	}
}
